<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 请把俩个数组[A1, A2, B1, B2, C1, C2, D1, D2] 和[A, B, C, D]，
        // 合并为[A1, A2, A, B1, B2, B, C1, C2, C, D1, D2, D]。

        var arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
        var arr2 = ['A', 'B', 'C', 'D'];

        // var newArr = [];
        // for(var i =0;i<arr2.length;i++){
        //     newArr.push(arr1.shift());
        //     newArr.push(arr1.shift());
        //     newArr.push(arr2[i]);
        // }
        // console.log(newArr); // ["A1", "A2", "A", "B1", "B2", "B", "C1", "C2", "C", "D1", "D2", "D"]

        var result = [];
        var temp = arr2[0]; // 保存arr2 中的元素
        var j = 0;
        for (var i = 0; i < arr1.length; i++) {
            if (arr1[i].indexOf(temp) !== -1) {
                // 假如 i==0 ,arr1[i]就是 A1 
                // 'A1'.indexOf('A') !== -1
                result.push(arr1[i]);
            } else {
                result.push(temp);
                result.push(arr1[i]);
                j++;
                temp = arr2[j]; // 修改 temp
            }
            console.log(result);
        }
        if (i === arr1.length) {
            result.push(temp);
        }
        console.log(result);
        
    </script>
</body>

</html>